package com.xiaoq.matrix.repository.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.matrix.entity.ContentMediaAccountEntity;
import com.xiaoq.matrix.repository.IContentMediaAccountRepository;


@Repository
public class ContentMediaAccountRepositoryImpl implements IContentMediaAccountRepository {
	@Autowired
	private Datastore datastore;
	
	@Autowired
	private IdGenerator idGenerator;

	private static final Logger LOG = LoggerFactory.getLogger(ContentMediaAccountEntity.class);
	@Override
	public boolean saveMediaAccount(ContentMediaAccountEntity contentMediaAccount) {

		String merchantId = contentMediaAccount.getMerchantId();
		String mediaId = contentMediaAccount.getMediaId();
		Query<ContentMediaAccountEntity> contentMediaAccountQuery = datastore
				.createQuery(ContentMediaAccountEntity.class).field("merchantId").equal(merchantId).field("mediaId")
				.equal(mediaId);
		ContentMediaAccountEntity contentMediaAccountQ = contentMediaAccountQuery.get();

		if (contentMediaAccountQ == null) {
			try {

				contentMediaAccount.setId(idGenerator.nextId());
				datastore.save(contentMediaAccount);
				return true;

			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		} else {
			
			UpdateOperations<ContentMediaAccountEntity> contentMediaUpdate = datastore
					.createUpdateOperations(ContentMediaAccountEntity.class)
					.set("token", contentMediaAccount.getToken())
					.set("refreshToken", contentMediaAccount.getRefreshToken())
					.set("tokenTime", contentMediaAccount.getTokenTime())
					.set("tokenDuratio", contentMediaAccount.getTokenDuratio())
					.set("updatedAt", new Date().getTime());
			datastore.update(contentMediaAccountQuery, contentMediaUpdate);

			return true;

		}
		
	}

	@Override
	public ContentMediaAccountEntity findCMA(String merchantId, String mediaId) {
		Query<ContentMediaAccountEntity> contentMediaAccountQuery = datastore
				.createQuery(ContentMediaAccountEntity.class).field("merchantId").equal(merchantId).field("mediaId")
				.equal(mediaId);
		
		ContentMediaAccountEntity contentMediaAccount = contentMediaAccountQuery.get();
		
		
		return contentMediaAccount;
	}

	@Override
	public boolean delMediaAccount(String merchantId, String mediaId) {
		if (StringUtils.isNotEmpty(merchantId) && StringUtils.isNotEmpty(mediaId)) {
			Query<ContentMediaAccountEntity> mediaAccountQuery = datastore.find(ContentMediaAccountEntity.class);
			mediaAccountQuery.field("merchantId").equal(merchantId).field("mediaId").equal(mediaId);
			datastore.delete(mediaAccountQuery);
			return true;
		}
		LOG.info("merchantId,mediaId任意值不能为空！");
		return false;
	}

}
